home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / libpng / pngrio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-12  |  4.4 KB  |  138 lines

  1.  
  2. /* pngrio.c - functions for data input
  3.  
  4.    libpng 1.0 beta 6 - version 0.96
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    Copyright (c) 1996, 1997 Andreas Dilger
  8.    May 12, 1997
  9.  
  10.    This file provides a location for all input.  Users which need
  11.    special handling are expected to write a function which has the same
  12.    arguments as this, and perform a similar function, but possibly has
  13.    a different input method.  Note that you shouldn't change this
  14.    function, but rather write a replacement function and then make
  15.    libpng use it at run time with png_set_read_fn(...) */
  16.  
  17. #define PNG_INTERNAL
  18. #include "png.h"
  19.  
  20. /* Read the data from whatever input you are using.  The default routine
  21.    reads from a file pointer.  Note that this routine sometimes gets called
  22.    with very small lengths, so you should implement some kind of simple
  23.    buffering if you are using unbuffered reads.  This should never be asked
  24.    to read more then 64K on a 16 bit machine. */
  25. void
  26. png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  27. {
  28.    png_debug1(4,"reading %d bytes\n", length);
  29.    if (png_ptr->read_data_fn != NULL)
  30.       (*(png_ptr->read_data_fn))(png_ptr, data, length);
  31.    else
  32.       png_error(png_ptr, "Call to NULL read function");
  33. }
  34.  
  35. /* This is the function which does the actual reading of data.  If you are
  36.    not reading from a standard C stream, you should create a replacement
  37.    read_data function and use it at run time with png_set_read_fn(), rather
  38.    than changing the library. */
  39. #ifndef USE_FAR_KEYWORD
  40. static void
  41. png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  42. {
  43.    png_size_t check;
  44.  
  45.    /* fread() returns 0 on error, so it is OK to store this in a png_size_t
  46.     * instead of an int, which is what fread() actually returns.
  47.     */
  48.    check = (png_size_t)fread(data, (png_size_t)1, length,
  49.       (FILE *)png_ptr->io_ptr);
  50.  
  51.    if (check != length)
  52.    {
  53.       png_error(png_ptr, "Read Error");
  54.    }
  55. }
  56. #else
  57. /* this is the model-independent version. Since the standard I/O library
  58.    can't handle far buffers in the medium and small models, we have to copy
  59.    the data.
  60. */
  61.  
  62. #define NEAR_BUF_SIZE 1024
  63. #define MIN(a,b) (a <= b ? a : b)
  64.  
  65. static void
  66. png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  67. {
  68.    int check;
  69.    png_byte *n_data;
  70.    FILE *io_ptr;
  71.  
  72.    /* Check if data really is near. If so, use usual code. */
  73.    n_data = (png_byte *)CVT_PTR_NOCHECK(data);
  74.    io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr);
  75.    if ((png_bytep)n_data == data)
  76.    {
  77.       check = fread(n_data, 1, length, io_ptr);
  78.    }
  79.    else
  80.    {
  81.       png_byte buf[NEAR_BUF_SIZE];
  82.       png_size_t read, remaining, err;
  83.       check = 0;
  84.       remaining = length;
  85.       do
  86.       {
  87.          read = MIN(NEAR_BUF_SIZE, remaining);
  88.          err = fread(buf, (png_size_t)1, read, io_ptr);
  89.          png_memcpy(data, buf, read); /* copy far buffer to near buffer */
  90.          if(err != read)
  91.             break;
  92.          else
  93.             check += err;
  94.          data += read;
  95.          remaining -= read;
  96.       }
  97.       while (remaining != 0);
  98.    }
  99.    if (check != length)
  100.    {
  101.       png_error(png_ptr, "read Error");
  102.    }
  103. }
  104. #endif
  105.  
  106. /* This function allows the application to supply a new input function
  107.    for libpng if standard C streams aren't being used.
  108.  
  109.    This function takes as its arguments:
  110.    png_ptr      - pointer to a png input data structure
  111.    io_ptr       - pointer to user supplied structure containing info about
  112.                   the input functions.  May be NULL.
  113.    read_data_fn - pointer to a new input function which takes as it's
  114.                   arguments a pointer to a png_struct, a pointer to
  115.                   a location where input data can be stored, and a 32-bit
  116.                   unsigned int which is the number of bytes to be read.
  117.                   To exit and output any fatal error messages the new write
  118.                   function should call png_error(png_ptr, "Error msg"). */
  119. void
  120. png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
  121.    png_rw_ptr read_data_fn)
  122. {
  123.    png_ptr->io_ptr = io_ptr;
  124.  
  125.    if (read_data_fn != NULL)
  126.       png_ptr->read_data_fn = read_data_fn;
  127.    else
  128.       png_ptr->read_data_fn = png_default_read_data;
  129.  
  130.    /* It is an error to write to a read device */
  131.    png_ptr->write_data_fn = NULL;
  132.  
  133. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  134.    png_ptr->output_flush_fn = NULL;
  135. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  136. }
  137.  
  138.